<template>
  <el-card class="area-card" shadow="hover">
    <template #header>
      <slot name="header">
        <!-- 默认标题插槽，可以被覆盖 -->
        <div class="card-header">
          <span class="area-name">{{ name }}</span>
          <div class="area-actions">
            <slot name="actions"></slot>
          </div>
        </div>
      </slot>
    </template>

    <div class="card-content">
      <!-- 左侧信息区域（插槽） - 仅在有内容时显示 -->
      <div v-if="hasInfoContent" class="info-section">
        <slot name="info"></slot>
      </div>

      <!-- 右侧表计列表 - 在有左侧内容时使用默认宽度，无左侧内容时占满 -->
      <div class="meters-section" :class="{ 'full-width': !hasInfoContent }">
        <!-- 水表 -->
        <div class="meter-box">
          <div class="meter-header">
            <div class="meter-title">水表</div>
          </div>
          <div class="meter-tags">
            <el-tag v-for="meter in waterMeters" :key="meter.meter_code" class="meter-tag">
              {{ meter.meter_code }}
            </el-tag>
            <div v-if="waterMeters.length === 0" class="no-data">暂无水表</div>
          </div>
        </div>

        <!-- 电表 -->
        <div class="meter-box">
          <div class="meter-header">
            <div class="meter-title">电表</div>
          </div>
          <div class="meter-tags">
            <el-tag v-for="meter in electricMeters" :key="meter.meter_code" class="meter-tag">
              {{ meter.meter_code }}
            </el-tag>
            <div v-if="electricMeters.length === 0" class="no-data">暂无电表</div>
          </div>
        </div>

        <!-- 气表 -->
        <div class="meter-box">
          <div class="meter-header">
            <div class="meter-title">气表</div>
          </div>
          <div class="meter-tags">
            <el-tag v-for="meter in gasMeters" :key="meter.meter_code" class="meter-tag">
              {{ meter.meter_code }}
            </el-tag>
            <div v-if="gasMeters.length === 0" class="no-data">暂无气表</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { ref, useSlots, computed, onMounted } from 'vue'

const props = defineProps({
  // 名称
  name: {
    type: String,
    default: ''
  },
  // 水表列表
  waterMeters: {
    type: Array,
    default: () => []
  },
  // 电表列表
  electricMeters: {
    type: Array,
    default: () => []
  },
  // 气表列表
  gasMeters: {
    type: Array,
    default: () => []
  }
})

// 获取插槽
const slots = useSlots()

// 计算是否有info插槽内容
const hasInfoContent = computed(() => {
  return !!slots.info
})
</script>

<style scoped>
.area-card {
  margin-bottom: 16px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.area-name {
  font-weight: bold;
  font-size: 16px;
}

.area-actions {
  display: flex;
  gap: 8px;
}

.card-content {
  display: flex;
  gap: 16px;
}

.info-section {
  min-width: 120px;
}

.meters-section {
  flex: 1;
  display: flex;
  justify-content: space-between;
  padding: 0 8px;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.meters-section.full-width {
  width: 100%;
}

.meter-box {
  width: 7rem;
  display: flex;
  flex-direction: column;
  height: 280px;
  padding: 16px;
}

.meter-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.meter-title {
  font-size: 16px;
  font-weight: bold;
}

.meter-tags {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: calc(100% - 28px);
  padding-right: 5px;
  overflow: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  touch-action: pan-y; /* 优化触摸设备的滚动 */
  -webkit-overflow-scrolling: touch; /* 在iOS设备上实现平滑滚动 */
}

.meter-tag {
  margin: 0;
  padding: 6px 10px;
}

.no-data {
  color: #909399;
  font-size: 14px;
  text-align: center;
  margin-top: 16px;
}
</style>